package com.liu.qqserver.server;

import com.liu.qqcommon.Message;
import com.liu.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

/**
 * 该类的对象和某个客户端保持连接
 */
public class ServerConnectClientThread extends Thread {
    private Socket socket;
    private String userId;//连接到服务器的用户ID
    public ServerConnectClientThread(Socket socket, String userId) {
        this.socket = socket;
        this.userId = userId;
    }
    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {//可以发送和接受消息
        while(true){

            try {
                System.out.println("服务端和"+userId+"客户端保持通讯,读取数据。。。");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message mes = (Message) ois.readObject();
                //根据Message类型做相应的业务逻辑
                if(mes.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)) {
                    //客户端请求在线列表
                    System.out.println(mes.getSender() + "要在线列表");
                    //将在线列表取出
                    String onlineUsers = ManageServerThreads.getOnlineUsers();
                    //开始返回给客户端Message信息
                    //构建Message
                    Message mes2 = new Message();
                    mes2.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                    mes2.setContent(onlineUsers);
                    //将接收者转换为客户端
                    mes2.setGetter(mes.getSender());
                    //返回客户端
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(mes2);
                } else if (mes.getMesType().equals(MessageType.MESSAGE_COMM_MES)) {
                    //根据Message中的getterId,调用getterId的线程
                    ServerConnectClientThread serverConnectClientThread = ManageServerThreads.getClientThread(mes.getGetter());
                    //得到对应的Socket对象的输出流，将Message对象转发给指定客户端
                    ObjectOutputStream oos = new ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream());
                    oos.writeObject(mes);//转发，如果客户端不在线可以设置成离线留言

                } else if(mes.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT )) {
                    System.out.println( mes.getSender()+"退出");
                    //将集合线程中需要结束的线程拿除
                    ManageServerThreads.removeClientThread(mes.getSender());
                    socket.close();
                    break;
                } else if (mes.getMesType().equals(MessageType.MESSAGE_TO_ALL_MES )) {
                    //遍历所有在线的线程
                    HashMap<String, ServerConnectClientThread> hm = ManageServerThreads.getHm();
                    Iterator<String> iterator = hm.keySet().iterator();
                    while(iterator.hasNext()){
                        //取出在线用户的Id
                        String onLineUserId = iterator.next().toString();
                        if(!onLineUserId.equals(mes.getSender())){
                            //进行转发
                            ObjectOutputStream oos = new ObjectOutputStream(hm.get(onLineUserId).getSocket().getOutputStream());
                            oos.writeObject(mes);
                        }
                    }

                } else if (mes.getMesType().equals(MessageType.MESSAGE_FILE_MES )) {
                     //根据getterId 获取到对应的线程，将Message转发
                    ServerConnectClientThread serverConnectClientThread = ManageServerThreads.getClientThread(mes.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream());
                    oos.writeObject(mes);
                } else{
                    System.out.println("别的业务暂时不处理");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }
}
